/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package data;

import exceptions.AuthException;
import java.math.BigInteger;
import java.util.HashMap;
import java.security.SecureRandom;

/**
 *
 * @author bertram
 */
public class TokenCache {

    private static HashMap<String, String> tokens = new HashMap<String, String>();
    private static SecureRandom random = new SecureRandom();

    public static String generateToken() {
        
        return new BigInteger(130, random).toString(32);
        
    }

    public static synchronized void addToken(String userName, String token) {
        tokens.put(token, userName);
    }

    public static boolean checkToken(String token)  {
        System.out.println("token: " + token);
        if (tokens.containsKey(token)) {
            return true;
        } else {
            
            return false;
        }
    }

    public static String getUserName(String token) throws AuthException {
        if (tokens.containsKey(token)) {
            return tokens.get(token);
        } else {
            throw new AuthException("invalid token");
        }
    }
}
